package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;


public class PingjiaDetailActivity extends BaseActivity implements View.OnClickListener,ContributorView {

    private ImageView regist_back;
    private TextView tv_headtitle;
    private String id;
    private TextView tv_pingjiasubmit;
    private ContributorPresenter contributorPresenter;
    private Context context;
    private final String UPDATESERVERBYIDFLAG="UPDATESERVERBYIDFLAG";
    private MyToast myToast;
    private EditText et_communt;
    private RatingBar ratingBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pingjiadetail_layout);
        initView();
        Intent intent=getIntent();
        context=this;
        myToast=new MyToast(context);
        contributorPresenter = new ContributorPresenter(this, context);
        id=intent.getStringExtra("id");
    }

    private void initView()
    {
        et_communt=(EditText) findViewById(R.id.et_communt);
        regist_back=(ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("业务详情");
        tv_headtitle.requestFocus();
        tv_headtitle.setFocusable(true);
        tv_headtitle.setFocusableInTouchMode(true);
        tv_pingjiasubmit=(TextView) findViewById(R.id.tv_pingjiasubmit);
        tv_pingjiasubmit.setOnClickListener(this);
        ratingBar=(RatingBar) findViewById(R.id.ratingBar);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
            //评分提交
            case R.id.tv_pingjiasubmit:
                String comment=et_communt.getText().toString().trim();
                int score=(int) ratingBar.getRating();
                if(score>0)
                {
                    contributorPresenter.getAssessApplyById(id,score,comment,UPDATESERVERBYIDFLAG);
                }else
                {
                    myToast.showShort("评分不能为空");
                }
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if(flag.equals(UPDATESERVERBYIDFLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = (String) object;
                    if(jsonMessage.equals("41"))
                    {
                        myToast.showShort("评价成功");
                        Intent intent=getIntent();
                        setResult(1,intent);
                        finish();
                    }else {
                        myToast.showShort("评价失败");
                    }
                }else
                {
                    myToast.showShort("评价失败");
                }
            }

        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }
}
